package com.deng.performance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.OperateDepartmentVo;
import com.deng.etl.impl.OperateMoveToBack;
import com.deng.etl.impl.WorkLoadMoveToBack;
import com.deng.performance.dao.IOperateFirstBonus;
import com.deng.util.DateTools;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class OperateFirstBonusImpl extends SQLHelper implements
		IOperateFirstBonus {

	
	public List<OperateDepartmentVo> calAllDispenseFirstBonus(
			List<OperateDepartmentVo> dvList) {
		return null;
	}

	public List<OperateDepartmentVo> calAllDispenseFirstBonusByOrganizationRecent(
			List<OperateDepartmentVo> dvList) {
		// TODO Auto-generated method stub
		return null;
	}

	private String getOperateOrgCode(Connection conn,int operate_id){
		try{
			
			String sql = "select original_id from t_sys_stat_operate_department_vs_original_department" +
					"	where operate_id = "+operate_id;
			String str = "";
			ResultSet rs = queryData(conn, sql);
			while(rs.next()){
				str += rs.getString(1)+",";
			}
			if(str.length() >0 ){
				return str.substring(0, str.length()-1);
			}
			return str;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<OperateDepartmentVo> getAllOperateFirstBonus(String statMonth) {
		try{
			Connection conn = getConnect();
			String sql = "select a.id,a.name,b.stat_time,b.related_id," +
					"	b.cal_value,b.pay_value,b.put_value,b.valid" +
					"	from t_sys_stat_operate_department as a" +
					"	left join t_stat_operate_month as b" +
					"	on b.operate_id = a.id" +
					"	and stat_time = '"+statMonth+"'";
			ResultSet rs = queryData(conn,sql);
			List<OperateDepartmentVo> l = new ArrayList<OperateDepartmentVo>();
			while(rs.next()){
				OperateDepartmentVo vo = new OperateDepartmentVo();
				vo.cal_value = rs.getBigDecimal("cal_value")!=null?rs.getBigDecimal("cal_value").toString():"0";
				vo.put_value = rs.getBigDecimal("put_value")!=null?rs.getBigDecimal("put_value").toString():"0";
				vo.pay_value = rs.getBigDecimal("pay_value")!=null?rs.getBigDecimal("pay_value").toString():"0";
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.stat_time = statMonth;
				vo.original_code = getOperateOrgCode(conn,vo.id);
				vo.valid = rs.getInt("valid");
				l.add(vo);
				
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	

	public OperateDepartmentVo recalByOldFunction(OperateDepartmentVo vo) {
		try{
			Connection conn = getConnect();
			
			String sql = "select * from t_sys_stat_operate_department where id = "+vo.id;
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				String formula = rs.getString("formula");
				if(formula == null || formula.equals("")){
					vo.status = "请检查是否已经设置公式，或者调整后公式未保存！";
					return vo;
				}
				formula = formula.replaceAll("@operate_id", vo.id+"");
				formula = formula.replaceAll("@stat_time", "'"+vo.stat_time+"'");
				formula = formula.replaceAll("\\[", "(").replaceAll("\\]", ")");
				System.out.println(" FORMULA:   "+formula);
				String querySQL = "select "+formula+" as x";
				System.out.println(" SQL:   "+querySQL);
				ResultSet rs2 = queryData(conn,querySQL);
				if(rs2.next()){
					vo.cal_value = rs2.getBigDecimal("x")!=null?rs2.getBigDecimal("x").toString():"0";
					return vo;
				}else{
					vo.status = "没有查询结果，请检验公式是否正确，或联系管理员!";
					return vo;
				}
			}
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public OperateDepartmentVo recalByNewFunction(OperateDepartmentVo vo) {
		try{
			Connection conn = getConnect();
			
			String sql = "select * from t_sys_stat_operate_department where id = "+vo.id;
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				String formula = rs.getString("formula");
				if(formula == null || formula.equals("")){
					vo.status = "请检查是否已经设置公式，或者调整后公式未保存！";
					return vo;
				}
				formula = formula.replaceAll("@operate_id", vo.id+"")
					.replaceAll("@stat_time", "'"+vo.stat_time+"'")
					.replaceAll("\\[", "(").replaceAll("\\]", ")")
					.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
					.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/");
				System.out.println(" FORMULA:   "+formula);
				String querySQL = "select "+formula+" as x";
				System.out.println(" SQL:   "+querySQL);
				ResultSet rs2 = queryData(conn,querySQL);
				if(rs2.next()){
					vo.cal_value = rs2.getBigDecimal("x")!=null?rs2.getBigDecimal("x").toString():"0";
					if(vo.cal_value != null){
						addOrUpdateCalValue(conn,vo);
					}
					System.out.println("经营绩效一次分配重新计算，将不对下级员工进行重新测算");
//					new OperateEmployeeImpl().updateByNewFunction(vo);
					return vo;
				}else{
					vo.status = "没有查询结果，请检验公式是否正确，或联系管理员!";
					return vo;
				}
			}
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	private void addOrUpdateCalValue(Connection conn,OperateDepartmentVo vo){
		try{
			String sql = "select * from t_stat_operate_month where operate_id ="+vo.id +" and stat_time = '"+vo.stat_time+"'";
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				if(rs.getInt("valid") == 1){
					return;
				}
				sql = "update t_stat_operate_month " +
				"	set cal_value = " +vo.cal_value+
				"	where operate_id = "+vo.id+" and stat_time = '"+vo.stat_time+"'";
			}else{
				sql = "insert into t_stat_operate_month " +
						"(operate_id,name,stat_time,related_id,cal_value,pay_value,put_value,valid) " +
						"values("+vo.id+",'"+vo.name+"','"+vo.stat_time+"','"+vo.original_code+"',"+
						vo.cal_value+","+vo.cal_value+","+vo.cal_value+",0)";
			}
			updateData(conn, sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}

	public void validOperateData(OperateDepartmentVo vo) {
		try{
			Connection conn = getConnect();
			String sql = "update t_stat_operate_month set valid = 1" +
					"	where stat_time = '"+vo.stat_time+"'" +
					"	and operate_id = "+vo.id;
			int x = updateData(conn,sql);
			if(x == 1){
				new OperateMoveToBack().operateMoveToBackByOperateId(vo.stat_time, vo.id);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	
	}
}
